// 防止页面加载时自动滚动
if (window.location.hash) {
    // 保持在顶部
    window.scrollTo(0, 0);
    
    // 当用户点击导航链接时才允许滚动
    document.querySelectorAll('a[href^="#"]').forEach(anchor => {
        anchor.addEventListener('click', function (e) {
            e.preventDefault();
            const target = document.querySelector(this.getAttribute('href'));
            if (target) {
                target.scrollIntoView({
                    behavior: 'smooth'
                });
            }
        });
    });
}

// 现有的全屏切换功能保持不变
function toggleFullscreen() {
    // ... existing code ...
}

// Mobile menu toggle
const mobileMenuButton = document.getElementById('mobile-menu-button');
const mobileMenu = document.getElementById('mobile-menu');

if (mobileMenuButton && mobileMenu) {
    mobileMenuButton.addEventListener('click', () => {
        mobileMenu.classList.toggle('hidden');
    });
}

// Mobile language switcher
const mobileLangSwitcher = document.getElementById('mobile-language-switcher');
const mobileLangDropdown = document.getElementById('mobile-language-dropdown');

if (mobileLangSwitcher && mobileLangDropdown) {
    mobileLangSwitcher.addEventListener('click', (e) => {
        e.stopPropagation();
        mobileLangDropdown.classList.toggle('hidden');
    });

    document.addEventListener('click', (e) => {
        if (!mobileLangSwitcher.contains(e.target)) {
            mobileLangDropdown.classList.add('hidden');
        }
    });
}

// Mobile theme toggle
const mobileThemeToggle = document.getElementById('mobile-theme-toggle');
const mobileThemeToggleLightIcon = document.getElementById('mobile-theme-toggle-light-icon');
const mobileThemeToggleDarkIcon = document.getElementById('mobile-theme-toggle-dark-icon');

if (mobileThemeToggle && mobileThemeToggleLightIcon && mobileThemeToggleDarkIcon) {
    // 初始化图标状态
    if (localStorage.getItem('color-theme') === 'dark' || 
        (!('color-theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
        mobileThemeToggleLightIcon.classList.remove('hidden');
        mobileThemeToggleDarkIcon.classList.add('hidden');
    } else {
        mobileThemeToggleLightIcon.classList.add('hidden');
        mobileThemeToggleDarkIcon.classList.remove('hidden');
    }

    // 切换主题
    mobileThemeToggle.addEventListener('click', () => {
        mobileThemeToggleDarkIcon.classList.toggle('hidden');
        mobileThemeToggleLightIcon.classList.toggle('hidden');

        if (localStorage.getItem('color-theme')) {
            if (localStorage.getItem('color-theme') === 'light') {
                document.documentElement.classList.add('dark');
                localStorage.setItem('color-theme', 'dark');
            } else {
                document.documentElement.classList.remove('dark');
                localStorage.setItem('color-theme', 'light');
            }
        } else {
            if (document.documentElement.classList.contains('dark')) {
                document.documentElement.classList.remove('dark');
                localStorage.setItem('color-theme', 'light');
            } else {
                document.documentElement.classList.add('dark');
                localStorage.setItem('color-theme', 'dark');
            }
        }
    });
}

// 点击其他区域关闭移动菜单
document.addEventListener('click', (e) => {
    if (mobileMenu && !mobileMenuButton.contains(e.target) && !mobileMenu.contains(e.target)) {
        mobileMenu.classList.add('hidden');
    }
});

// Desktop language switcher
const desktopLangSwitcher = document.getElementById('desktop-language-switcher');
const desktopLangDropdown = document.getElementById('desktop-language-dropdown');

if (desktopLangSwitcher && desktopLangDropdown) {
    desktopLangSwitcher.addEventListener('click', (e) => {
        e.stopPropagation();
        desktopLangDropdown.classList.toggle('hidden');
    });

    document.addEventListener('click', (e) => {
        if (!desktopLangSwitcher.contains(e.target)) {
            desktopLangDropdown.classList.add('hidden');
        }
    });
}

// Desktop theme toggle
const desktopThemeToggle = document.getElementById('desktop-theme-toggle');
const desktopThemeToggleLightIcon = document.getElementById('desktop-theme-toggle-light-icon');
const desktopThemeToggleDarkIcon = document.getElementById('desktop-theme-toggle-dark-icon');

if (desktopThemeToggle && desktopThemeToggleLightIcon && desktopThemeToggleDarkIcon) {
    // 初始化图标状态
    if (localStorage.getItem('color-theme') === 'dark' || 
        (!('color-theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
        desktopThemeToggleLightIcon.classList.remove('hidden');
        desktopThemeToggleDarkIcon.classList.add('hidden');
    } else {
        desktopThemeToggleLightIcon.classList.add('hidden');
        desktopThemeToggleDarkIcon.classList.remove('hidden');
    }

    // 切换主题
    desktopThemeToggle.addEventListener('click', () => {
        desktopThemeToggleDarkIcon.classList.toggle('hidden');
        desktopThemeToggleLightIcon.classList.toggle('hidden');

        if (localStorage.getItem('color-theme')) {
            if (localStorage.getItem('color-theme') === 'light') {
                document.documentElement.classList.add('dark');
                localStorage.setItem('color-theme', 'dark');
            } else {
                document.documentElement.classList.remove('dark');
                localStorage.setItem('color-theme', 'light');
            }
        } else {
            if (document.documentElement.classList.contains('dark')) {
                document.documentElement.classList.remove('dark');
                localStorage.setItem('color-theme', 'light');
            } else {
                document.documentElement.classList.add('dark');
                localStorage.setItem('color-theme', 'dark');
            }
        }
    });
}